// Acrobat Headers.
#ifndef MAC_PLATFORM
#include <PIHeaders.h>
#endif

#include <PIMain.h> // for gHINSTANCE
#include "resource.h"
#include "MetadataManager.h"
#include "MetadataDlg.h"
#include "SelectionManager.h"
#include "AcronymManager.h"
#include "TagButtons.h"
#include "AboutDlg.h"
#include <htmlhelp.h>


const char* MyPluginExtensionName = "TASC:Extrakt";
const char* TagToolbarId = "TASC:ExtraktTagToolbar";

CString configPath;

AcronymDictionary acronymDictionary;
TagButtons tagButtons;

EXTERN_C IMAGE_DOS_HEADER __ImageBase;

/*-------------------------------------------------------
	Functions
-------------------------------------------------------*/

ACCB1 ASBool ACCB2 MyPluginSetmenu();
ACCB1 ASBool ACCB2 CreateTagToolbar();
ACCB1 ASBool ACCB2 SetUpToolButtons();

//================================================================================
ACCB1 ASBool ACCB2 ExtraktPluginInit(void)
{
	TCHAR path [_MAX_PATH];
	TCHAR drive[_MAX_DRIVE];
	TCHAR dir  [_MAX_DIR];

	GetModuleFileName((HINSTANCE)&__ImageBase, path, _MAX_PATH);

	_wsplitpath_s(path,				drive,	_MAX_DRIVE,	dir,   _MAX_DIR, 0,	0,	0,	0);
	_wmakepath_s (path,	_MAX_PATH,	drive,				dir,             0,	    0);

	configPath = CString(path) + _T("Extrakt\\");

	CString acronymFile = configPath + _T("Acronym.xml");

	if (acronymDictionary.Load(acronymFile) == false)
	{
		CString errorMsg;
		errorMsg.Format(_T("Error reading acronym list (%s)"), acronymFile);

		//CAVAlert::DisplayNote(errorMsg);
		return false;
	}

	CString tagFile = configPath + _T("Tags.xml");

	if(tagButtons.Load(tagFile) == false)
	{
		CString errorMsg;
		errorMsg.Format(_T("Error reading tag definition (%s)"), tagFile);

		//CAVAlert::DisplayNote(errorMsg);
		return false;
	}

	MyPluginSetmenu();
	CreateTagToolbar();
	SetUpToolButtons();

	return true;
}

ACCB1 ASBool ACCB2 ExtraktPluginUnload(void)
{
	//if (menuItem)
	//	AVMenuItemRemove(menuItem);

	return true;
}

//================================================================================


ACCB1 ASBool ACCB2 MyPluginIsEnabled(void *clientData)
{
	 return (AVAppGetActiveDoc() != NULL);
}

ACCB1 ASBool ACCB2 TagButtonIsEnabled(void *clientData)
{
	AVDoc avDoc = AVAppGetActiveDoc();

	return (avDoc != NULL) && (AVDocGetSelectionType(avDoc) == Text_K);
}


ACCB1 ASBool ACCB2 CreateTagToolbar()
{
	// Checking if the toolbar has already been created
	AVToolBar toolBar = AVAppGetToolBarByName (TagToolbarId);

	// If it doesn't then create it
	if ( toolBar == NULL)
	{
		// Create structure to populate
		AVToolBarPosition myAVToolBarPosition = new AVToolBarPositionRec();

		// file in the parameters required to create a toolbar position
		myAVToolBarPosition->size = sizeof(AVToolBarPositionRec);
		myAVToolBarPosition->inDoc = false;
		myAVToolBarPosition->dockPosition = kAVToolBarDockTop;
		myAVToolBarPosition->floatingWindowName = NULL;
		myAVToolBarPosition->stackNum = 1;
		myAVToolBarPosition->offset = -1;
		myAVToolBarPosition->order = -1;
		myAVToolBarPosition->hidden = false;
		myAVToolBarPosition->windowHidden = false;

		// This is all that is actually required to create a new toolbar, the AVToolBarPosition
		// tells Acrobat how to display the toolbar
		toolBar = AVToolBarNew ( TagToolbarId, "Extrakt Tag Toolbar");

		// Set the position information of the toolbar.
		AVAppRegisterToolBarPosition ( TagToolbarId, false, myAVToolBarPosition);
	}
	return true;
}


ACCB1 void ACCB2 TagCommand(void *clientData)
{
	AVDoc avDoc = AVAppGetActiveDoc();

	if(avDoc==NULL) return;

	int i = (int) clientData;

	SelectionExplorer selectionExplorer(avDoc);

	SelectionManager selectionManager(avDoc, tagButtons.Items[i].Name, tagButtons.Items[i].ShowCustomizationDialog, acronymDictionary, &selectionExplorer);

	selectionExplorer.ExploreSelectedText(selectionManager);
}

ACCB1 ASBool ACCB2 SetUpToolButtons()
{
	AVToolBar toolBar = AVAppGetToolBarByName (TagToolbarId);
	if ( toolBar == NULL) return false;

	//void *stamperIcon = (AVCursor)LoadBitmap(gHINSTANCE, MAKEINTRESOURCE(IDB_BITMAP1));;

	for(unsigned int i = 0; i < tagButtons.Items.size(); i++)
	{
		AVToolButton button = AVToolButtonNew (ASAtomFromString2(tagButtons.Items[i].Id), tagButtons.Items[i].hBitmap, true, false);

		AVToolButtonSetExecuteProc (button, TagCommand, (void*)i);
		AVToolButtonSetComputeEnabledProc (button, TagButtonIsEnabled, (void *)pdPermEdit);
		//AVToolButtonSetComputeMarkedProc (button, cbStamperIsMarked, NULL);
		//AVToolButtonSetHelpText (button, tagButtons.Items[i].Hint);


		AVToolBarAddButton(toolBar, button, false, NULL);
	}

	return true;
}

ACCB1 void ACCB2 MetadataCommand(void *clientData)
{
	AVDoc avDoc = AVAppGetActiveDoc();

	if(avDoc == NULL) return;

	PDDoc pdDoc = AVDocGetPDDoc (avDoc);
		
	MetadataManager metadataManager(pdDoc);
	MetadataDialog(metadataManager);
}

ACCB1 void ACCB2 PageDescriptions(void *clientData)
{
	AVDoc avDoc = AVAppGetActiveDoc();

	if(avDoc == NULL) return;

	PDDoc pdDoc = AVDocGetPDDoc (avDoc);

	int numPages = PDDocGetNumPages(pdDoc);

	ASInt32 firstPage;
	ASInt32 lastPage;

	PDDocGetPageLabel (pdDoc, 0, &firstPage, &lastPage);

	if(firstPage == -1 || lastPage == -1) return;
	
	while( lastPage < numPages -1 )
	{
		PDPageLabel pageLabel = PDDocGetPageLabel(pdDoc, lastPage +1, &firstPage, &lastPage);
		PDDocRemovePageLabel (pdDoc, firstPage);		
	}
}

ACCB1 void ACCB2 AcronymCommand(void *clientData)
{
	AVDoc avDoc = AVAppGetActiveDoc();
	if(avDoc == NULL) return;

	SelectionExplorer selectionExplorer(avDoc);

	AcronymManager acronymManager(avDoc, acronymDictionary, &selectionExplorer);

	selectionExplorer.ExploreText(acronymManager);
}

ACCB1 void ACCB2 HelpCommand(void *clientData)
{
	HtmlHelp(NULL, configPath + _T("Extrakt.Help.chm"), 0, NULL);
}

ACCB1 void ACCB2 AboutCommand(void *clientData)
{
	AboutDlg about;

	about.ShowDialog();
}


/* MyPluginSetmenu
** ------------------------------------------------------
**
** Function to set up menu for the plug-in.
** It calls a convenient function PluginMenuItem.
** Return true if successful, false if failed.
*/
ACCB1 ASBool ACCB2 MyPluginSetmenu()
{
	//Retrieve the menu bar in Adobe Reader or Acrobat
	AVMenubar menubar = AVAppGetMenubar();
	
	if (!menubar)
		return false;

	bool res = true;

	AVMenu newMenu = NULL;
	AVMenuItem metadataItem      = NULL;
	AVMenuItem renumberPagesItem = NULL;
	AVMenuItem acronymItem       = NULL;
	AVMenuItem helpItem          = NULL;
	AVMenuItem aboutItem         = NULL;

	DURING	
		//Create a new menu
		newMenu = AVMenuNew("Extrakt", "TASC:ExtraktMenu", gExtensionID);
		
		//Create a new menu command
		acronymItem = AVMenuItemNew("Shortcuts and Items scan", "TASC:Items", NULL, true, NO_SHORTCUT, 0, NULL, gExtensionID);
		AVMenuItemSetExecuteProc (acronymItem, ASCallbackCreateProto(AVExecuteProc, AcronymCommand), NULL);
		AVMenuItemSetComputeEnabledProc (acronymItem, ASCallbackCreateProto(AVComputeEnabledProc, MyPluginIsEnabled), (void *)pdPermEdit);

		//Create a new menu command
		renumberPagesItem = AVMenuItemNew("Renumber Pages", "TASC:RenumberPages", NULL, true, NO_SHORTCUT, 0, NULL, gExtensionID);		
		AVMenuItemSetExecuteProc (renumberPagesItem, ASCallbackCreateProto(AVExecuteProc, PageDescriptions), NULL);
		AVMenuItemSetComputeEnabledProc (renumberPagesItem, ASCallbackCreateProto(AVComputeEnabledProc, MyPluginIsEnabled), (void *)pdPermEdit);

		//Create a new menu command
		metadataItem = AVMenuItemNew("Metadata", "TASC:Metadata", NULL, true, NO_SHORTCUT, 0, NULL, gExtensionID);		
		AVMenuItemSetExecuteProc (metadataItem, ASCallbackCreateProto(AVExecuteProc, MetadataCommand), NULL);
		AVMenuItemSetComputeEnabledProc (metadataItem, ASCallbackCreateProto(AVComputeEnabledProc, MyPluginIsEnabled), (void *)pdPermEdit);

		helpItem = AVMenuItemNew("Help", "TASC:Help", NULL, true, NO_SHORTCUT, 0, NULL, gExtensionID);
		AVMenuItemSetExecuteProc (helpItem, ASCallbackCreateProto(AVExecuteProc, HelpCommand), NULL);

		aboutItem = AVMenuItemNew("About", "TASC:About", NULL, true, NO_SHORTCUT, 0, NULL, gExtensionID);	
		AVMenuItemSetExecuteProc (aboutItem, ASCallbackCreateProto(AVExecuteProc, AboutCommand), NULL);

		//Attach the menu item to the menu and the menu to
		//the menu bar
		AVMenuAddMenuItem (newMenu, metadataItem, 0);
		AVMenuAddMenuItem (newMenu, renumberPagesItem, 1);
		AVMenuAddMenuItem (newMenu, acronymItem, 2);
		AVMenuAddMenuItem (newMenu, helpItem, 3);
		AVMenuAddMenuItem (newMenu, aboutItem, 4);
		
		AVMenubarAddMenu (menubar, newMenu, APPEND_MENU);

	HANDLER
		res = false;
	END_HANDLER

	if (aboutItem)         AVMenuItemRelease(aboutItem);
	if (helpItem)          AVMenuItemRelease(helpItem);
	if (acronymItem)       AVMenuItemRelease(acronymItem);
	if (renumberPagesItem) AVMenuItemRelease(renumberPagesItem);
	if (metadataItem)      AVMenuItemRelease(metadataItem);
	if (newMenu)           AVMenuRelease (newMenu);

	return res;
}